home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / jcool01.zip / TEST_HAS.C < prev    next >
C/C++ Source or Header  |  1992-09-28  |  12KB  |  273 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Updated: JAM 08/18/92 -- modernize template syntax, remove macro hacks
  13.  
  14. #include <cool/String.h>
  15. #include <cool/Vector.h>
  16. #include <cool/Hash_Table.h>
  17. #include <test.h>
  18.  
  19. #include <cool/Vector.C>
  20. #include <cool/Hash_Table.C>
  21.  
  22. // get and put pass values by reference.  Cfront creates stack
  23. // temporaries for these values, which creates too many
  24. // nesting levels for the Microsoft C compiler.
  25.  
  26. void test_int_double_get_put(CoolHash_Table<int,double>& h0) {
  27.   int i;
  28.   double d;
  29.   static int c1=1, c2=2, c3=3, c4=4, c5=5, c6=6, c7=7, c8=8, c9=9;
  30.   TEST ("h0.reset()",(h0.reset(),c1),c1);
  31.   TEST ("h0.put(1,2.5)", h0.put(c1,2.5),TRUE);
  32.   TEST ("h0.value()", h0.value(), 2.5);
  33.   TEST ("h0.get(1,d)", (h0.get(c1,d) == TRUE && d == 2.5), TRUE);
  34.   TEST ("h0.length()", h0.length(), c1);
  35.   TEST ("h0.value()", h0.value(), 2.5);
  36.   TEST ("h0.put(2,3.5)", h0.put(c2,3.5),TRUE);
  37.   TEST ("h0.get(2,d)", (h0.get(c2,d) == TRUE && d == 3.5), TRUE);
  38.   TEST ("h0.put(3,4.5)", h0.put(c3,4.5),TRUE);
  39.   TEST ("h0.get(3,d)", (h0.get(c3,d) == TRUE && d == 4.5), TRUE);
  40.   TEST ("h0.put(4,5.5)", h0.put(c4,5.5),TRUE); 
  41.   TEST ("h0.value()", h0.value(), 5.5);
  42.   TEST ("h0.get(4,d)", (h0.get(c4,d) == TRUE && d == 5.5), TRUE);
  43.   TEST ("h0.value()", h0.value(), 5.5);
  44.   TEST ("h0.put(5,9.5)", h0.put(c5,6.5),TRUE);
  45.   TEST ("h0.get(5,d)", (h0.get(c5,d) == TRUE && d == 6.5), TRUE);
  46.   TEST ("h0.value()", h0.value(), 6.5);
  47.   TEST ("h0.put(6,7.5)", h0.put(c6,7.5),TRUE);
  48.   TEST ("h0.get(6,d)", (h0.get(c6,d) == TRUE && d == 7.5), TRUE);
  49.   TEST ("h0.get_key(7.5,i)", h0.get_key(7.5,i), TRUE);
  50.   TEST ("i == 6", (i == c6), 1);
  51.   TEST ("h0.put(7,8.5)", h0.put(c7,8.5),TRUE);
  52.   TEST ("h0.get(7,d)", (h0.get(c7,d) == TRUE && d == 8.5), TRUE);
  53.   TEST ("h0.put(8,9.5)", h0.put(c8,9.5),TRUE);
  54.   TEST ("h0.get(8,d)", (h0.get(c8,d) == TRUE && d == 9.5), TRUE);
  55.   TEST ("h0.put(9,10.5)", h0.put(c9,10.5),TRUE);
  56.   TEST ("h0.get(9,d)", (h0.get(c9,d) == TRUE && d == 10.5), TRUE);
  57.   TEST ("h0.get_bucket_count()", h0.get_bucket_count(), 3);
  58.   TEST ("h0.length()", h0.length(), 9);
  59.   TEST ("h0.remove(1)", h0.remove(1), TRUE);
  60.   TEST ("h0.length()", h0.length(), 8);
  61. }
  62.  
  63. void test_int_double_next(CoolHash_Table<int,double>& h0) {
  64.   TEST ("h0.reset()",(h0.reset(),1),1);
  65.   TEST ("h0.next()",h0.next(),TRUE);
  66.   TEST ("h0.next()",h0.next(),TRUE);
  67.   TEST ("h0.next()",h0.next(),TRUE);
  68.   TEST ("h0.next()",h0.next(),TRUE);
  69.   TEST ("h0.next()",h0.next(),TRUE);
  70.   TEST ("h0.next()",h0.next(),TRUE);
  71.   TEST ("h0.next()",h0.next(),TRUE);
  72.   TEST ("h0.next()",h0.next(),TRUE);
  73.   TEST ("h0.next()",h0.next(),FALSE);
  74.   TEST ("h0.prev()",h0.prev(),TRUE);
  75.   TEST ("h0.prev()",h0.prev(),TRUE);
  76.   TEST ("h0.prev()",h0.prev(),TRUE);
  77.   TEST ("h0.prev()",h0.prev(),TRUE);
  78.   TEST ("h0.prev()",h0.prev(),TRUE);
  79.   TEST ("h0.prev()",h0.prev(),TRUE);
  80.   TEST ("h0.prev()",h0.prev(),TRUE);
  81.   TEST ("h0.prev()",h0.prev(),TRUE);
  82.   TEST ("h0.prev()",h0.prev(),FALSE);
  83. }
  84.  
  85. void test_int_double () {
  86.   int i;
  87.   double d;
  88.   // get and put pass values by reference.  Cfront creates stack
  89.   // temporaries for these values, which creates too many
  90.   // nesting levels for the Microsoft C compiler.
  91.   static int c2=2, c3=3, c4=4, c6=6, c7=7, c8=8, c9=9;
  92.   CoolHash_Table<int,double> h0;
  93.   TEST ("CoolHash_Table<int,double> h0",h0.get_bucket_count(),3);
  94.   CoolHash_Table<int,double> h1(20);
  95.   TEST ("CoolHash_Table<int,double> h1(20)",h1.get_bucket_count(),3);
  96.   CoolHash_Table<int,double> h2(25);
  97.   TEST ("CoolHash_Table<int,double> h2(25)",h2.get_bucket_count(),7);
  98.   CoolHash_Table<int,double> h3(h2);
  99.   TEST ("CoolHash_Table<int,double> h3(h2)",h3.get_bucket_count(),7);
  100.   TEST ("h2==h3", (h2==h3), TRUE);
  101.   TEST ("h2 == h3", (h2 == h3), TRUE);
  102.   TEST ("h1 == h2", (h1 == h2), TRUE);
  103.   TEST ("h1 != h3", (h1 != h3), FALSE);
  104.   test_int_double_get_put(h0);
  105.   test_int_double_next(h0);
  106.   TEST ("h1=h0", (h1=h0,(h0==h1)), TRUE);
  107.   h1.resize(30); //  TEST ("h1.resize(30)", h1.resize(30), TRUE);
  108.   TEST ("h1.get(7,d)", (h1.get(c7,d) == TRUE && d == 8.5), TRUE);
  109.   TEST ("h1.value()", h1.value(), 8.5);
  110.   TEST ("h1.get(3,d)", (h1.get(c3,d) == TRUE && d == 4.5), TRUE);
  111.   TEST ("h1.value()", h1.value(), 4.5);
  112.   TEST ("h1.get(9,d)", (h1.get(c9,d) == TRUE && d == 10.5), TRUE);
  113.   TEST ("h1.value()", h1.value(), 10.5);
  114.   TEST ("h1.get_bucket_count()", h1.get_bucket_count(), 7);
  115.   TEST ("h1.length()", h1.length(), 8);
  116.   TEST ("h0.set_ratio(1.0)", (h0.set_ratio(1.0),1), 1);
  117.   h0.resize(30); //  TEST ("h0.resize(30)", h0.resize(30), TRUE);
  118.   TEST ("h0.get_bucket_count()", h0.get_bucket_count(), 7);
  119.   TEST ("h0.get(2,d)", (h0.get(c2,d) == TRUE && d == 3.5), TRUE);
  120.   TEST ("h0.get(4,d)", (h0.get(c4,d) == TRUE && d == 5.5), TRUE);
  121.   TEST ("h0.get(6,d)", (h0.get(c6,d) == TRUE && d == 7.5), TRUE);
  122.   TEST ("h0.get(8,d)", (h0.get(c8,d) == TRUE && d == 9.5), TRUE);
  123.   h0.resize(60); // TEST ("h0.resize(60)", h0.resize(60), TRUE);
  124.   TEST ("h0.get_bucket_count()", h0.get_bucket_count(), 19);
  125.   TEST ("h0.get(2,d)", (h0.get(c2,d) == TRUE && d == 3.5), TRUE);
  126.   TEST ("h0.value()", h0.value(), 3.5);
  127.   TEST ("h0.get(4,d)", (h0.get(c4,d) == TRUE && d == 5.5), TRUE);
  128.   TEST ("h0.value()", h0.value(), 5.5);
  129.   TEST ("h0.get(6,d)", (h0.get(c6,d) == TRUE && d == 7.5), TRUE);
  130.   TEST ("h0.value()", h0.value(), 7.5);
  131.   TEST ("h0.get(8,d)", (h0.get(c8,d) == TRUE && d == 9.5), TRUE);
  132.   TEST ("h0.get_key(9.5,i)", h0.get_key(9.5,i), TRUE);
  133.   TEST ("i == 8", (i == 8), 1);
  134.   TEST ("h0.value()", h0.value(), 9.5);
  135.   TEST ("h0.clear()", (h0.clear(), h0.length()), 0);
  136.   TEST ("h1.clear()", (h1.clear(), h1.length()), 0);
  137.   TEST ("h3.clear()", (h3.clear(), h3.length()), 0);
  138.   TEST ("for(i=0;i<100;i++) h3.put(i,double(i))",1,1);
  139.   for(i = 0; i < 100; i++)
  140.      h3.put(i, (double) i);
  141.   TEST ("for(i=0;i<100;i++) h3.find(i)",1,1);
  142.   for(i = 0; i < 100; i++)
  143.     if(h3.find(i) == FALSE) {
  144.       TEST ("for(i=0;i<100;i++) h3.find(i)",h3.find(i),TRUE);
  145.       break;
  146.     }
  147. }
  148.  
  149.  
  150. void test_double_String () {
  151.   CoolString s;
  152.   // get and put pass values by reference.  Cfront creates stack
  153.   // temporaries for these values, which creates too many
  154.   // nesting levels for the Microsoft C compiler.
  155.   static double d1=1, d2=2, d3=3, d4=4, d5=5, d6=6, d7=7, d8=8, d9=9;
  156.   CoolHash_Table<double,CoolString> h0;
  157.   TEST ("CoolHash_Table<double,CoolString> h0",h0.get_bucket_count(),3);
  158.   CoolHash_Table<double,CoolString> h1(20);
  159.   TEST ("CoolHash_Table<double,CoolString> h1(20)",h1.get_bucket_count(),3);
  160.   CoolHash_Table<double,CoolString> h2(25);
  161.   TEST ("CoolHash_Table<double,CoolString> h2(25)",h2.get_bucket_count(),7);
  162.   CoolHash_Table<double,CoolString> h3(h2);
  163.   TEST ("CoolHash_Table<double,CoolString> h3(h2)",h3.get_bucket_count(),7);
  164.   TEST ("h2==h3",(h2==h3), TRUE);
  165.   TEST ("h2 == h3", (h2 == h3), TRUE);
  166.   TEST ("h1 == h2", (h1 == h2), TRUE);
  167.   TEST ("h1 != h3", (h1 != h3), FALSE);
  168.   TEST ("h0.put(1.0,CoolString(\"ABCD\"))", h0.put(d1,CoolString("ABCD")),TRUE);
  169.   TEST("h0.get(1.0,s)",(h0.get(d1,s)==TRUE && strcmp(s,"ABCD")==0),TRUE);
  170.   TEST ("h0.length()", h0.length(), 1);
  171.   TEST ("h0.put(2.0,CoolString(\"EFGH\"))", h0.put(d2,CoolString("EFGH")),TRUE);
  172.   TEST("h0.get(2.0,s)",(h0.get(d2,s)==TRUE && strcmp(s,"EFGH")==0),TRUE);
  173.   TEST ("h0.put(3.0,CoolString(\"IJKL\"))", h0.put(d3,CoolString("IJKL")),TRUE);
  174.   TEST("h0.get(3.0,s)",(h0.get(d3,s)==TRUE && strcmp(s,"IJKL")==0),TRUE);
  175.   TEST ("h0.put(4.0,CoolString(\"MNOP\"))", h0.put(d4,CoolString("MNOP")),TRUE);
  176.   TEST("h0.get(4.0,s)",(h0.get(d4,s)==TRUE && strcmp(s,"MNOP")==0),TRUE);
  177.   TEST ("h0.put(5.0,CoolString(\"QRsT\"))", h0.put(d5,CoolString("QRST")),TRUE);
  178.   TEST("h0.get(5.0,s)",(h0.get(d5,s)==TRUE && strcmp(s,"QRST")==0),TRUE);
  179.   TEST ("h0.put(6.0,CoolString(\"UVWX\"))", h0.put(d6,CoolString("UVWX")),TRUE);
  180.   TEST("h0.get(6.0,s)",(h0.get(d6,s)==TRUE && strcmp(s,"UVWX")==0),TRUE);
  181.   // TEST ("h0.get_key(\"QRST\",d)", h0.get_key("QRST",d), TRUE);
  182.   // TEST ("d == 5.0", (d == 5.0), 1);
  183.   TEST ("h0.put(7.0,CoolString(\"YZab\"))", h0.put(d7,CoolString("YZab")),TRUE);
  184.   TEST("h0.get(7.0,s)",(h0.get(d7,s)==TRUE && strcmp(s,"YZab")==0),TRUE);
  185.   TEST ("h0.put(8.0,CoolString(\"cdef\"))", h0.put(d8,CoolString("cdef")),TRUE);
  186.   TEST("h0.get(8.0,s)",(h0.get(d8,s)==TRUE && strcmp(s,"cdef")==0),TRUE);
  187.   TEST ("h0.put(9.0,CoolString(\"ghij\"))", h0.put(d9,CoolString("ghij")),TRUE);
  188.   TEST("h0.get(9.0,s)",(h0.get(d9,s)==TRUE && strcmp(s,"ghij")==0),TRUE);
  189.   TEST ("h0.get_bucket_count()", h0.get_bucket_count(), 3);
  190.   TEST ("h0.length()", h0.length(), 9);
  191.   TEST ("h0.remove(1.0)", h0.remove(1.0), TRUE);
  192.   TEST ("h0.length()", h0.length(), 8);
  193.   TEST ("h1=h0", (h1=h0,(h0==h1)), TRUE);
  194.   h1.resize(30); //  TEST ("h1.resize(30)", h1.resize(30), TRUE);
  195.   TEST("h1.get(7.0,s)",(h1.get(d7,s)==TRUE && strcmp(s,"YZab")==0),TRUE);
  196.   TEST("h1.get(3.0,s)",(h1.get(d3,s)==TRUE && strcmp(s,"IJKL")==0),TRUE);
  197.   TEST("h1.get(9.0,s)",(h1.get(d9,s)==TRUE && strcmp(s,"ghij")==0),TRUE);
  198.   TEST ("h1.get_bucket_count()", h1.get_bucket_count(), 7);
  199.   TEST ("h1.length()", h1.length(), 8);
  200.   TEST ("h0.set_ratio(1.0)", (h0.set_ratio(1.0),1), 1);
  201.   h0.resize(30); //  TEST ("h0.resize(30)", h0.resize(30), TRUE);
  202.   TEST ("h0.get_bucket_count()", h0.get_bucket_count(), 7);
  203.   TEST("h0.get(2.0,s)",(h0.get(d2,s)==TRUE && strcmp(s,"EFGH")==0),TRUE);
  204.   TEST("h0.get(4.0,s)",(h0.get(d4,s)==TRUE && strcmp(s,"MNOP")==0),TRUE);
  205.   TEST("h0.get(6.0,s)",(h0.get(d6,s)==TRUE && strcmp(s,"UVWX")==0),TRUE);
  206.   TEST("h0.get(8.0,s)",(h0.get(d8,s)==TRUE && strcmp(s,"cdef")==0),TRUE);
  207.   h0.resize(60); //  TEST ("h0.resize(60)", h0.resize(60), TRUE);
  208.   TEST ("h0.get_bucket_count()", h0.get_bucket_count(), 19);
  209.   TEST("h0.get(2.0,s)",(h0.get(d2,s)==TRUE && strcmp(s,"EFGH")==0),TRUE);
  210.   TEST("h0.get(4.0,s)",(h0.get(d4,s)==TRUE && strcmp(s,"MNOP")==0),TRUE);
  211.   TEST("h0.get(6.0,s)",(h0.get(d6,s)==TRUE && strcmp(s,"UVWX")==0),TRUE);
  212.   TEST("h0.get(8.0,s)",(h0.get(d8,s)==TRUE && strcmp(s,"cdef")==0),TRUE);
  213.   // TEST ("h0.get_key(\"cdef\",d)", h0.get_key("cdef",d), TRUE);
  214.   // TEST ("d == 8.0", (d == 8.0), 1);
  215.   TEST ("h0.clear()", (h0.clear(), h0.length()), 0);
  216.   TEST ("h1.clear()", (h1.clear(), h1.length()), 0);
  217. }
  218.  
  219. template<class Type>
  220. unsigned long CoolVector_hash(const CoolVector<Type>& key) {
  221.    return key.length(); //##
  222. }
  223.  
  224. unsigned long CoolHash_Table_default_hash (const CoolVector<int>& key) {
  225.    return key.length(); //##
  226. }
  227.  
  228. void test_Vector_int_String () {
  229.   CoolString s;
  230.   CoolString s1("Hello World");
  231.   TEST ("CoolString s1(\"Hello World\")",0,0);
  232.   CoolString s2("Up, up, and away!");
  233.   TEST ("CoolString s2(\"Up, up, and away!\")",0,0);
  234.   CoolVector<int> v1(5, 5, 1, 1, 1, 1, 1);
  235.   TEST ("CoolVector<int> v1(5,1)",(v1[0]==1 && v1[1]==1 && v1[2]==1 && v1[3]==1 && v1[4]==1),1);
  236.   CoolVector<int> v2(8);
  237.   TEST ("CoolVector<int> v2(8)",0,0);
  238.   v2.set_length(8);
  239.   TEST ("v2.set_length(8)", v2.length(), 8);
  240.   for (int i = 0; i < 8; i++)
  241.     v2[i] = i;
  242.   CoolHash_Table<CoolVector<int>,CoolString> h1;
  243.   TEST ("CoolHash_Table<CoolVector<int>,CoolString> h1",h1.get_bucket_count(),3);
  244.   TEST ("h1.put(v1,s1)", h1.put(v1,s1), TRUE);
  245.   TEST ("h1.get(v1,s)", (h1.get(v1,s)==TRUE && strcmp (s,s1)==0), TRUE);
  246.   TEST ("h1.length()", h1.length(), 1);
  247.   TEST ("h1.put(v2,s2)", h1.put(v2,s2), TRUE);
  248.   TEST ("h1.put(v2,s2) again", h1.put(v2,s2), FALSE);
  249.   TEST ("h1.get(v2,s)", (h1.get(v2,s)==TRUE && strcmp (s,s2)==0), TRUE);
  250.   TEST ("h1.length()", h1.length(), 2);
  251. }
  252.  
  253. void test_leak() {
  254.   for (;;) {
  255.     test_int_double ();
  256.     test_double_String  ();
  257.     test_Vector_int_String ();
  258.   }
  259. }
  260.  
  261.  
  262. int main (void) {
  263.   START("CoolHash_Table");
  264.   test_int_double ();
  265.   test_double_String  ();
  266.   test_Vector_int_String ();
  267. #if LEAK
  268.   test_leak ();
  269. #endif
  270.   SUMMARY();
  271.   return 0;
  272. }
  273.